/**
* @class
* jquery.Popup.js Layer Popup OPEN, CLOSE
*
* @name Popup
* @author JsYang <yakuyaku@gmail.com>
* @since 2009-10-16
* @version 2.0
*/

;(function($) {

    var status = {};
    var ie6   = $.browser.msie && parseInt($.browser.version) == 6 && typeof window['XMLHttpRequest'] != "object";

    $.fn.Popup = function(options)
    {
    var opts = options;
    $(this).bind('click.Popup', function(){
    return new $modal(opts);
    });
    };

    $.Popup = function(options) {
    return new $modal(options);
    };

    $.Popup.defaults = {
    name  : '#popupLayer' ,
    closeButton : '#close' ,
    backgroundClose : true,
    backgroundDisplay  : true ,
    backgroundOpacity  : "0.5" ,
    dragHandle : '.modal_win_pop_header' ,
    dragContainment : 'document',
    center : true ,
    fade   : true ,
    speed  : 'fast' ,
    zIndex : '100'  ,
    left  : false  ,
    top   : false  ,
    onShow : false  ,
    onOpen : false  ,
    onClose : false
    };


    $.Popup.impl = function(options)
    {
    var s   = this;
    s.opts  = $.extend({}, $.Popup.defaults, options);
    s.popup = $(s.opts.name);
    s.closeButton = $(s.opts.closeButton);
    s.init();
    s.bindEvents();
    };

    var $modal;

    $modal = $.Popup.impl;

    $modal.fn = $modal.prototype = {
        version : '1.3'
    };

    $modal.fn.extend =  $modal.extend = $.extend;

    $modal.fn.extend({
    init : function(){
    var s =this, center_flag= true, pos;

    s.status = 0;

    pos = $modal.moveCenter(s.popup);

    if (s.opts.left && s.opts.top == false) {
    center_flag = false;
    s.opts.top = pos.top;
    } else if (s.opts.top && s.opts.left == false) {
    center_flag = false;
    s.opts.left = pos.left;
    } else if ( s.opts.left && s.opts.top ) {
    center_flag = false;
    }

    if( center_flag ) {
    s.opts.top  = pos.top;
    s.opts.left = pos.left;
    }

    s.popup.css({
    "position": "absolute",
    "zIndex" :  this.opts.zIndex ,
    "top"    :  this.opts.top    ,
    "left"   :  this.opts.left
    });

    if(s.opts.backgroundDisplay){
    s.makeOverlay();
    if(ie6) $modal.fixIE(this.overlay);
    }

    if(ie6) {
    s.makeIFrame();
    }

    s.show();

    if ($.isFunction(s.opts.onShow)) {
    s.opts.onShow.apply(s, [s.popup]);
    }

    },
    show : function(){
    var s=this;
    if(s.status == 0)  {

    if ($.isFunction(s.opts.onOpen)) {
    s.opts.onOpen.apply(s, [s.popup]);
    } else {
    if(s.opts.fade) {
    s.popup.fadeIn(s.opts.speed);
    if(s.opts.backgroundDisplay)  s.overlay.fadeIn(s.opts.speed);
    } else {
    s.popup.css('display', 'block');
    if(s.opts.backgroundDisplay)  s.overlay.css('display', 'block');
    }
    }

    if(ie6 && s.ifrm) {
    s.ifrm.css({
    top :  s.popup.offset().top ,
    left : s.popup.offset().left
    });
    }

    if( jQuery.ui && jQuery.ui.draggable && s.opts.backgroundClose) {
    s.popup.draggable({
    handle : s.opts.dragHandle,
    containment : s.opts.dragContainment,
    drag: function(event, ui) {
    if(ie6) {
    s.ifrm.css({
    top  : $(this).offset().top ,
    left : $(this).offset().left
    });
    }
    }
    });
    }
    s.status = 1;
    }
    },
    close : function() {
    var s=this;
    if(s.status == 1 ) {

    if ($.isFunction(s.opts.onClose)) {
    s.opts.onClose.apply(s, [s.popup]);
    } else {
    if(s.opts.fade) {
    s.popup.fadeOut(this.opts.speed);
    if(s.opts.backgroundDisplay)  s.overlay.fadeOut(this.opts.speed);
    } else {
    s.popup.css('display', 'none');
    if(s.opts.backgroundDisplay)  s.overlay.css('display', 'none');
    }
    }
    if(ie6 && s.ifrm) {
    s.ifrm.hide().remove();
    }
    s.status = 0;
    }
    },
    makeOverlay: function() {
    var overlayName = "backgroundOverlay";
    if( $('.' + overlayName).length < 1 )
    {
    this.overlay = $("<div class='"+ overlayName + "'></div>");
    this.overlay.css({
    "display" : "none" ,
    "position" : "fixed" ,
    "height" : "100%" ,
    "width"  : "100%" ,
    "left" : "0",
    "top"  : "0",
    "background" : "#000" ,
    "zIndex" : "3" ,
    "opacity": this.opts.backgroundOpacity
    });
    $('body').prepend(this.overlay);
    } else {
    this.overlay = $("." + overlayName);
    }
    },
    makeIFrame : function() {
    var s = this;
    $('#iframe_bg').remove();
    if(!s.ifrm) {
    s.ifrm =  $("<iframe src='about:blank' id='iframe_bg' src='about:blank' scrolling='no' frameborder='0' ></iframe>")
    .css({
    'position' : 'absolute',
    'width'    : s.popup.width() ,
    'height'   : s.popup.height(),
    'opacity'  : '0',
    'border'   : 'none'
    }).insertBefore(s.popup);
    }
    },
    bindEvents : function(){
    var s = this;

    s.closeButton.bind("click.closeButton", function(e){
    e.preventDefault();
    s.close();
    });

    $(document).bind('keydown.layerPopup', function (e) {
    if ( s.status == 1 && e.keyCode == 27) { // ESC
    e.preventDefault();
    s.close();
    };
    });

    if (s.opts.backgroundClose) {
    $(s.overlay).bind("click.overlay",function(e){
    if ( s.status == 1 ) {
    e.preventDefault();
    s.close();
    };
    });
    }

    },
    unbindEvents : function(){
    $(this.opts.closeButton).unbind('click.closeButton');
    $(document).unbind('keydown.layerPopup');
    $(this.opts.overlay).unbind('click.overlay');
    }
    });

    $modal.extend({
    moveCenter : function(modal) {
    var x,y;
    x =  $(window).width()/2-modal.width()/2;
    y =  $(window).scrollTop() + $(window).height()/2-modal.height()/2;
    return { left : x, top: y};
    },

    fixIE: function (objs) {
    $.each([objs], function (i, el) {
    if (el) {
    var bch = 'document.body.clientHeight', bcw = 'document.body.clientWidth',
    bsh = 'document.body.scrollHeight', bsl = 'document.body.scrollLeft',
    bst = 'document.body.scrollTop', bsw = 'document.body.scrollWidth',
    ch = 'document.documentElement.clientHeight', cw = 'document.documentElement.clientWidth',
    sl = 'document.documentElement.scrollLeft', st = 'document.documentElement.scrollTop',
    s = el[0].style;

    el.css('position', 'absolute');

    if (i < 2) {
        el.css('height', $(document).height() + 'px');
                        el.css('width', $(document).width() + 'px');
    }
    else {
    var te, le;
    if (p && p.constructor == Array) {
    var top = p[0]
    ? typeof p[0] == 'number' ? p[0].toString() : p[0].replace(/px/, '')
    : el.css('top').replace(/px/, '');
    te = top.indexOf('%') == -1
    ? top + ' + (t = ' + st + ' ? ' + st + ' : ' + bst + ') + "px"'
    : parseInt(top.replace(/%/, '')) + ' * ((' + ch + ' || ' + bch + ') / 100) + (t = ' + st + ' ? ' + st + ' : ' + bst + ') + "px"';

    if (p[1]) {
    var left = typeof p[1] == 'number' ? p[1].toString() : p[1].replace(/px/, '');
    le = left.indexOf('%') == -1
    ? left + ' + (t = ' + sl + ' ? ' + sl + ' : ' + bsl + ') + "px"'
    : parseInt(left.replace(/%/, '')) + ' * ((' + cw + ' || ' + bcw + ') / 100) + (t = ' + sl + ' ? ' + sl + ' : ' + bsl + ') + "px"';
    }
    }
    else {
    te = '(' + ch + ' || ' + bch + ') / 2 - (this.offsetHeight / 2) + (t = ' + st + ' ? ' + st + ' : ' + bst + ') + "px"';
    le = '(' + cw + ' || ' + bcw + ') / 2 - (this.offsetWidth / 2) + (t = ' + sl + ' ? ' + sl + ' : ' + bsl + ') + "px"';
    }
    s.removeExpression('top');
    s.removeExpression('left');
    s.setExpression('top', te);
    s.setExpression('left', le);
    }
    }
    });
    }

    });

})(jQuery);
